package com.blood.modules.system.controller;

import com.blood.core.annotation.Idempotent;
import com.blood.core.resp.GlobalResultStatus;
import com.blood.core.resp.PageResult;
import com.blood.core.resp.ResponseResult;
import com.blood.modules.system.dto.req.SysUpdatePasswordReq;
import com.blood.modules.system.dto.req.SysUserPageReq;
import com.blood.modules.system.dto.req.SysUserReq;
import com.blood.modules.system.dto.req.SysUserSaveReq;
import com.blood.modules.system.dto.resp.ResetPasswordResp;
import com.blood.modules.system.dto.resp.SysUserInfoResp;
import com.blood.modules.system.dto.resp.SysUserListResp;
import com.blood.modules.system.dto.resp.SysUserPageResp;
import com.blood.modules.system.model.SysUser;
import com.blood.modules.system.service.ISysUserService;
import com.github.xiaoymin.knife4j.annotations.ApiSort;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * 系统用户Controller
 *
 * @author daoren
 * @date 2022-05-12 09:14:48
 */
@Api(value = "人员管理", tags = "人员管理")
@ApiSort(905)
@RestController
@RequestMapping("/sys/user")
public class SysUserController {
    /**
     * 系统用户服务.
     */
    @Autowired
    private ISysUserService sysUserService;

    /**
     * 分页查询列表
     *
     * @param req
     * @return 分页数据
     */
    @PostMapping("/pageList")
    @ApiOperation(value = "系统用户列表-分页")
    public ResponseResult<PageResult<SysUserPageResp>> pageList(@RequestBody SysUserPageReq req) {
        return sysUserService.pageList(req);
    }

    /**
     * 获取用户列表信息.
     *
     * @param req 请求参数
     * @return 列表信息
     */
    @ApiOperation(value = "获取用户列表信息")
    @PostMapping(value = "/getListInfo")
    public ResponseResult<List<SysUserListResp>> getListInfo(@RequestBody SysUserReq req) {
        List<SysUser> queryList = sysUserService.queryList(req);
        ArrayList<SysUserListResp> list = new ArrayList<>();
        for (SysUser sysUser : queryList) {
            SysUserListResp resp = new SysUserListResp();
            resp.setId(sysUser.getId());
            resp.setUserName(sysUser.getEmpNo() + "~" + sysUser.getUserName());
            list.add(resp);
        }
        return GlobalResultStatus.SUCCESS.toResult().response(list);
    }

    @ApiOperation(value = "添加系统用户信息")
    @PostMapping(value = "/add")
    @Idempotent
    public ResponseResult<Void> add(@RequestBody @Validated SysUserSaveReq req) {
        return sysUserService.saveInfo(req);
    }

    @ApiOperation(value = "修改系统用户信息")
    @PostMapping(value = "/update")
    @Idempotent
    public ResponseResult<Void> update(@RequestBody @Validated SysUserSaveReq req) {
        return sysUserService.updateInfo(req);
    }

    @ApiOperation(value = "修改密码")
    @PostMapping(value = "/updatePassword")
    @Idempotent
    public ResponseResult<Void> updatePassword(@RequestBody @Validated SysUpdatePasswordReq req) {
        return sysUserService.updatePassword(req);
    }

    /**
     * 根据id获取详情
     *
     * @param id
     * @return 详情
     */
    @GetMapping("/findDetailById")
    @ApiOperation(value = "获取系统用户详情")
    public ResponseResult<SysUserInfoResp> findDetailById(@ApiParam(value = "id", type = "String") @RequestParam("id") String id) {
        return sysUserService.findDetailById(id);
    }

    /**
     * 重置密码
     *
     * @param id 用户账号
     * @return 返回账户信息
     */
    @ApiOperation(value = "重置密码")
    @GetMapping(value = "/reset")
    public ResponseResult<ResetPasswordResp> resetPassword(@ApiParam(value = "id", type = "String") @RequestParam("id") String id) {
        return sysUserService.resetPassword(id);
    }

    /**
     * 更改账号状态
     *
     * @param id 用户账号
     * @return 返回账户信息
     */
    @ApiOperation(value = "更改账号状态")
    @GetMapping(value = "/updateStatus")
    public ResponseResult<Void> updateStatus(@ApiParam(value = "用户id", type = "String") @RequestParam("id") String id
    ,@ApiParam(value = "修改状态（1正常，2禁用，3离职）", type = "Integer") @RequestParam("status") Integer status) {
        return sysUserService.updateStatus(id,status);
    }
    /**
     * 根据id删除数据
     *
     * @param id
     * @return 详情
     */
    @GetMapping("/deleted")
    @ApiOperation(value = "删除数据")
    public ResponseResult<Void> deleted(@ApiParam(value = "用户id", type = "String") @RequestParam("id") String id) {
        return sysUserService.deleted(id);
    }
}
